package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupItemMapper;
import com.itheima.health.dao.CheckGroupMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessException;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckGroupItem;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@DubboService
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {


    @Autowired
    CheckGroupMapper checkGroupMapper;

    @Autowired
    CheckGroupItemMapper checkGroupItemMapper;
    /**
     * @Author: JASON
     * @Date: 16:17 2021/1/4
     * @Parms [queryPageBean]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 检查组分页查询
     */

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        log.info("Service检查组分页查询"+queryPageBean);
        //分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupMapper.findByPage(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());


        return pageResult;
    }

    /**
     * @Author: JASON
     * @Date: 16:45 2021/1/4
     * @Parms [checkGroup]
     * @ReturnType: void
     * @Description: 新增检查组
    */
    @Override
    public void add(CheckGroup checkGroup,Integer[] checkItemIds) {
      log.info("Service新增检查组"+checkGroup);
      checkGroupMapper.insertSelective(checkGroup);
        // 2 插入关联表
        for (Integer checkItemId : checkItemIds) {
            checkGroupMapper.insertCheckGroupAndCheckItem(checkGroup.getId(), checkItemId);
        }
    }

    /**
     * @Author: JASON
     * @Date: 17:07 2021/1/4
     * @Parms [id]
     * @ReturnType: com.itheima.health.pojo.CheckGroup
     * @Description: 根据id查询检查组
    */
    @Override
    public CheckGroup findById(Integer id) {
        log.info("Service根据id查询检查组");
        return checkGroupMapper.selectByPrimaryKey(id);
    }

    /**
     * @Author: JASON
     * @Date: 17:12 2021/1/4
     * @Parms [checkGroup]
     * @ReturnType: void
     * @Description: 根据id查询检查组
    */
    @Override
    public void edit(CheckGroup checkGroup,Integer[] checkitemIds) {
        log.info("Service根据id修改检查组"+checkGroup);
        checkGroupMapper.updateByPrimaryKeySelective(checkGroup);
        //删除原有关系
        checkGroupMapper.deleteCheckGroupAndCheckItemByCheckGroupId(checkGroup.getId());
        //添加新的关系
        for (Integer checkItemId : checkitemIds) {
            checkGroupMapper.insertCheckGroupAndCheckItem(checkGroup.getId(), checkItemId);
        }
    }

    /**
     * @Author: JASON
     * @Date: 17:16 2021/1/4
     * @Parms [id]
     * @ReturnType: void
     * @Description: 根据id删除检查组
    */
    @Override
    public void delete(Integer id) {
        log.info("Service根据id删除检查组"+id);
        List<Integer> list = this.findCheckItemIdsByCheckGroupId(id);
        if(list.size()!=0){
            throw new BusinessException("检查组下有关联的检查项不能被删除");
        }
        checkGroupMapper.deleteByPrimaryKey(id);
    }

    /**
     * @Author: JASON
     * @Date: 15:55 2021/1/5
     * @Parms [id]
     * @ReturnType: java.util.List<java.lang.Integer>
     * @Description: 根据groupid查询checkitemID
    */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        List<Integer> list=checkGroupItemMapper.findCheckItemIdsByCheckGroupId(id);
        log.info("根据groupid查询checkitemID的结果为"+list);
        return list;
    }


    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroups = checkGroupMapper.selectAll();
        return checkGroups;
    }

}
